package com.lanou.beautifulwardrobe.Home.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.activity.ProductDetailActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.DetailData;
import com.lanou.beautifulwardrobe.entify.DetailItemImgs;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class DetailViewPagerAdapter extends PagerAdapter {

    private List<DetailItemImgs> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private Context context;

    public DetailViewPagerAdapter(Context c) {
        context = c;
    }

    public void addData(List<DetailItemImgs> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        final ImageView imageView = new ImageView(context);
        if (datas != null && datas.size() > 0) {
            volleySingleton.getImageResuest(datas.get(position).getUrl(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    imageView.setImageBitmap(response);
                    container.addView(imageView);
                }
            });
        }
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
